<?php
/**
 * 第三方登录接口封装类
 * @author liujz
 */
class connect_class 
{
	/**
	 * Connect Sina
	 */
	function connectSina($p)
	{
		$req 		= array('type'=>'sina', 'source'=>vApi::source(), '_ip'=>vApi::ip());
		$call_back 	= API_VYOUJI_API_URL.'thd/connect_callback.smpauth?'.VF::signRequest($req);
		$oWeibo 	= vApi::getClass('sinaapi/sinaWeibo');
		$authToken	= $oWeibo->getRequestToken($call_back);
		if (empty($authToken['oauth_token']) || empty($authToken['oauth_token_secret'])) {
			vApi::throwException('417102', '获取Request Token 失败');
		}
		
		// Set Cache
		$oCache	  			= vApi::getBase('apiCache');
		$cachekey 			= $this->getAuthTokenCacheKey('sina', $authToken['oauth_token']);
		$authToken['_cbk'] 	= vApi::param($p,'callback', '');
		$rst	  			= $oCache->set('authToken3rd', $cachekey, $authToken);
		if ( !$rst ) { vApi::throwException('417103', '缓存Request Token 失败'); }
		
		// Redirect
		$display = vApi::param($p, 'display', 'page');
		$authUrl = $oWeibo->getAuthorizeURL($authToken, $call_back, $display);
		VF::redirect($authUrl);
	}
	
	
	/**
	 * Connect QQ
	 */
	function connectQq($p)
	{
		$req 		= array('type'=>'qq', 'source'=>vApi::source(), '_ip'=>vApi::ip());
		$call_back 	= API_VYOUJI_API_URL.'thd/connect_callback.smpauth?'.VF::signRequest($req);
		$oWeibo 	= vApi::getClass('qqapi/qqWeibo');
		$authToken	= $oWeibo->getRequestToken($call_back);
		if (empty($authToken['oauth_token']) || empty($authToken['oauth_token_secret'])) {
			vApi::throwException('417102', '获取Request Token 失败');
		}
		
		// Set Cache
		$oCache	  			= vApi::getBase('apiCache');
		$cachekey 			= $this->getAuthTokenCacheKey('qq', $authToken['oauth_token']);
		$authToken['_cbk'] 	= vApi::param($p,'callback','');
		$rst	 	 		= $oCache->set('authToken3rd', $cachekey, $authToken);
		if ( !$rst ) { vApi::throwException('417103', '缓存Request Token 失败'); }
		
		// Redirect
		$display = vApi::param($p, 'display', 'page');
		$authUrl = $oWeibo->getAuthorizeURL($authToken,$call_back, $display);
		VF::redirect($authUrl);
	}
	
	
	/**
	 * Connect Renren
	 */
	function connectRenren($p)
	{
		$_cbk 		= vApi::param($p,'callback');
		$req		= array('type'=>'renren', '_cbk'=>$_cbk, 'source'=>vApi::source(), '_ip'=>vApi::ip());
		$call_back 	= API_VYOUJI_API_URL.'thd/connect_callback.smpauth?'.VF::signRequest($req);
		$oWeibo 	= vApi::getClass('renrenapi/renrenWeibo');
		
		// Redirect
		$display = vApi::param($p, 'display', 'page');
		$authUrl = $oWeibo->getAuthorizeURL($call_back, $display);
		VF::redirect($authUrl);
	}
	
	
	/**
	 * Binding Sina
	 */
	function bindSina($p)
	{
		// 是否已绑定
		$uid 	= vApi::uid();
		$checkU	= vApi::getBase('user_bind')->get3PartUser(1, $uid);
		if ( !empty($checkU) ) { vApi::throwException('417305', '帐号已绑定'); }
		
		// Build Call Back Url
		$_cbk 		= vApi::param($p,'callback','');
		$req		= array('type'=>'sina', 'source'=>vApi::source(), '_ip'=>vApi::ip());
		$call_back 	= API_VYOUJI_API_URL.'thd/bind_callback.smpauth?'.VF::signRequest($req);
		
		// Get AuthToken
		$oWeibo 	= vApi::getClass('sinaapi/sinaWeibo');
		$authToken	= $oWeibo->getRequestToken($call_back);
		if (empty($authToken['oauth_token']) || empty($authToken['oauth_token_secret'])) {
			vApi::throwException('417302', '获取Request Token 失败');
		}
		
		// Set Cache
		$oCache	  			= vApi::getBase('apiCache');
		$cachekey 			= $this->getAuthTokenCacheKey('sina', $authToken['oauth_token']);
		$authToken['_cbk'] 	= $_cbk;
		$authToken['uid'] 	= $uid;
		$rst	  			= $oCache->set('authToken3rd', $cachekey, $authToken);
		if ( !$rst ) { vApi::throwException('417303', '缓存Request Token 失败'); }
		
		// Redirect
		$display = vApi::param($p, 'display', 'page');
		$authUrl = $oWeibo->getAuthorizeURL($authToken, $call_back, $display);
		VF::redirect($authUrl);
	}
	
	
	/**
	 * Binding QQ
	 */
	function bindQq($p)
	{
		// 是否已绑定
		$uid 	= vApi::uid();
		$checkU	= vApi::getBase('user_bind')->get3PartUser(2, $uid);
		if ( !empty($checkU) ) { vApi::throwException('417305', '帐号已绑定'); }
		
		// Build Call Back Url
		$_cbk 		= vApi::param($p,'callback','');
		$source		= vApi::source();
		$req		= array('type'=>'qq', 'source'=>$source, '_ip'=>vApi::ip());
		$call_back 	= API_VYOUJI_API_URL.'thd/bind_callback.smpauth?'.VF::signRequest($req);
		
		// Get AuthToken
		$oWeibo 	= vApi::getClass('qqapi/qqWeibo');
		$authToken	= $oWeibo->getRequestToken($call_back);
		if (empty($authToken['oauth_token']) || empty($authToken['oauth_token_secret'])) {
			vApi::throwException('417302', '获取Request Token 失败');
		}
		
		// Set Cache
		$oCache	  			= vApi::getBase('apiCache');
		$cachekey 			= $this->getAuthTokenCacheKey('qq', $authToken['oauth_token']);
		$authToken['_cbk'] 	= $_cbk;
		$authToken['uid'] 	= $uid;
		$rst	  			= $oCache->set('authToken3rd', $cachekey, $authToken);
		if ( !$rst ) { vApi::throwException('417303', '缓存Request Token 失败'); }
		
		// Redirect
		$display = vApi::param($p, 'display', 'page');
		$authUrl = $oWeibo->getAuthorizeURL($authToken,$call_back, $display);
		VF::redirect($authUrl);
	}
	
	
	/**
	 * Binding RenRen
	 */
	function bindRenren($p)
	{
		// 是否已绑定
		$uid 	= vApi::uid();
		$checkU	= vApi::getBase('user_bind')->get3PartUser(3, $uid);
		if ( !empty($checkU) ) { vApi::throwException('417305', '帐号已绑定'); }
		
		// Build Call Back Url
		$_cbk 		= vApi::param($p,'callback');
		$source		= vApi::source();
		$req		= array('type'=>'renren', '_cbk'=>$_cbk, 'source'=>$source, 'b'=>$uid, '_ip'=>vApi::ip());
		$call_back 	= API_VYOUJI_API_URL.'thd/bind_callback.smpauth?'.VF::signRequest($req);
		$oWeibo 	= vApi::getClass('renrenapi/renrenWeibo');
		
		// Redirect
		$display = vApi::param($p, 'display', 'page');
		$authUrl = $oWeibo->getAuthorizeURL($call_back, $display);
		VF::redirect($authUrl);
	}
	
	// 获取Token 的Cache key
	function getAuthTokenCacheKey($type, $authToken) {
		return TOKEN_PASS_KEY."#$type#$authToken";
	}
}